package com.learner.vod.service.impl;

import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.learner.common.exception.ExceptionUtil;
import com.learner.common.exception.RestException;
import com.learner.pojo.media.Video;
import com.learner.vod.handle.AliyunVodSdkHandle;
import com.learner.vod.handle.VodProperties;
import com.learner.vod.repository.VideoRepository;
import com.learner.vod.service.VideoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.InputStream;

/**
* @author ice2020x
* @Date: 2023/3/11
* @Description:
**/
@Service
@Slf4j
public class VideoServiceImpl implements VideoService {

    @Autowired
    private VodProperties vodProperties;

    @Autowired
    private VideoRepository videoRepository;


    @Override
    public Video uploadVideo(InputStream inputStream, String originalFilename, Long size) {

        String title = originalFilename.substring(0, originalFilename.lastIndexOf("."));
        String fileExt = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
        UploadStreamRequest request = new UploadStreamRequest(vodProperties.getKeyid(),
                vodProperties.getKeysecret(),
                title,
                originalFilename,
                inputStream
        );
        UploadVideoImpl uploadVideo = new UploadVideoImpl();
        // 开始上传
        UploadStreamResponse response = uploadVideo.uploadStream(request);
        String videoSourceId = response.getVideoId();
        Video video = new Video();
        video.setVideoSourceId(videoSourceId);
        video.setVideoOriginalName(originalFilename);
        video.setFileType(fileExt);
        video.setSize(size);
        video.setTitle(title);
        videoRepository.save(video);
        // 如果上传失败抛出错误
        if (StringUtils.isEmpty(videoSourceId)) {
            log.error("阿里云上传失败: " + response.getCode() + "--" + response.getMessage());
            throw ExceptionUtil.createException(RestException.class, 4300003);
        }
        return video;
    }

    @Override
    public void removeById(String videoId) {
        DefaultAcsClient client;
        try {
            client = AliyunVodSdkHandle.initVodClient(vodProperties.getKeyid(), vodProperties.getKeysecret());
            DeleteVideoRequest request = new DeleteVideoRequest();
            request.setVideoIds(videoId);
            client.getAcsResponse(request);
            videoRepository.deleteById(videoId);
        } catch (ClientException e) {
            log.error("视频删除失败", e);
            throw ExceptionUtil.createException(RestException.class, 4300002);
        }
    }

    @Override
    public String getPlayAuth(String videoSourceId) {
        DefaultAcsClient client;
        try {
            client = AliyunVodSdkHandle.initVodClient(vodProperties.getKeyid(), vodProperties.getKeysecret());
            GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
            // 设置资源id
            request.setVideoId(videoSourceId);
            // 设置超时时间
            request.setAuthInfoTimeout(200L);
            GetVideoPlayAuthResponse response = client.getAcsResponse(request);
            return response.getPlayAuth();
        } catch (ClientException e) {
            log.error("获取播放凭证失败", e);
            throw ExceptionUtil.createException(RestException.class, 4300001);
        }
    }


}
